`
https://leetcode.cn/problems/combination-sum/
`

/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */
var combinationSum = function (candidates, target) {
  const res = []
  const track = []
  let trackSum = 0

  const backtrack = (start) => {
    if (trackSum === target) {
      res.push([...track])
      return
    }

    if (trackSum > target) {
      return
    }

    for (let i = start; i < candidates.length; i++) {
      track.push(candidates[i])
      trackSum += candidates[i]
      // 由于每个数字可以被无限制重复选取，所以递归还是从当前元素开始
      backtrack(i)
      track.pop()
      trackSum -= candidates[i]
    }
  }

  backtrack(0)
  return res
};